package onion.molingo2o.activity.request;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;

import com.easemob.chatuidemo.ui.BaseActivity;

import java.io.File;
import java.util.ArrayList;

import onion.molingo2o.R;
import onion.molingo2o.activity.ablumimage.AlbumActivity;
import onion.molingo2o.activity.ablumimage.ImagePagerActivity;
import onion.molingo2o.adapter.ImageAdapter;
import onion.molingo2o.utils.FileUtils;
import onion.molingo2o.utils.ImageUtils;
import onion.molingo2o.utils.SelectImgUtil;
import onion.molingo2o.utils.imageutil.BimpRotateUtil;
import onion.molingo2o.utils.imageutil.BitmapHelper;

/**
 * 需求详情页
 * 甲乙双方共有
 * Created by Administrator on 2015/12/20.
 */
public class RequestDetailActivity extends BaseActivity {
    SelectImgUtil selectImgUtil;
    GridView gv_request;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
    setContentView(R.layout.activity_reqdetail);
        selectImgUtil=new SelectImgUtil(this);
        gv_request= (GridView) findViewById(R.id.gv_request);
        ArrayList<String>  imgUrls=new ArrayList<>();
        imgUrls.add("http://pica.nipic.com/2007-11-09/200711912453162_2.jpg");
        imgUrls.add("http://pica.nipic.com/2007-11-09/200711912453162_2.jpg");
        imgUrls.add("http://pica.nipic.com/2007-11-09/200711912453162_2.jpg");
        imgUrls.add("http://pica.nipic.com/2007-11-09/200711912453162_2.jpg");
        imgUrls.add("http://pica.nipic.com/2007-11-09/200711912453162_2.jpg");
    gv_request.setAdapter(new ImageAdapter(this,imgUrls));

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == AlbumActivity.resultCode_to_NewActivity) {
            if (data != null) {
                selectImgUtil.  refreshGridView();
            }
        } else if (resultCode == ImagePagerActivity.resultCode_to_newAcitivity) {
            selectImgUtil. refreshGridView();
        }
        if (requestCode == SelectImgUtil.requestCode_to_Photo) {
            Log.i("onion", "data" + data);
            String photoName=selectImgUtil.photoName;
            if (data != null) { //可能尚未指定intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                //返回有缩略图
                if (data.hasExtra("data")) {

                    Bitmap thumbnail = data.getParcelableExtra("data");
                    //Bitmap thumbnail= null;
                    try {
                        int degree = BimpRotateUtil.getBitmapDegree(photoName);
                        thumbnail = BimpRotateUtil.rotateBitmapByDegree(thumbnail, degree);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //得到bitmap后的操作
                    FileUtils.saveBitmap(thumbnail, photoName);
                    // FileUtils.saveMyBitmap(photoName,thumbnail );
                    File f = new File(photoName);
                    if (f.exists()) {
                        ImageUtils.rotate(BitmapFactory.decodeFile(photoName), ImageUtils.readPictureDegree(photoName));
                        BitmapHelper.imageListStorage.add(photoName);
                        selectImgUtil.refreshGridView();
                    }
                }
            } else {
                //由于指定了目标uri，存储在目标uri，intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                // 通过目标uri，找到图片
                // 对图片的缩放处理
                // 操作
                ImageUtils.rotate(BitmapFactory.decodeFile(photoName), ImageUtils.readPictureDegree(photoName));
                BitmapHelper.imageListStorage.add(photoName);
                selectImgUtil.refreshGridView();
            }

        }else
        if (requestCode == SelectImgUtil.requestCode_to_imagePager) {
            selectImgUtil.refreshGridView();
        }
    }
}
